package com.wellsoft.mobileoa;

import java.util.HashMap;
import java.util.Map;

import org.androidpn.client.Constants;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;

import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Response;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.ServiceResponse;

public class NotifyDetailActivity extends BaseActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		url = intent.getStringExtra(Constants.NOTIFICATION_URI);
		AlertDialog alertDialog = showWaitingDialog();
		processUrl(url);
		alertDialog.dismiss();
	}

	private void processUrl(String url) {
		WSOA_HttpGet_Request request = new WSOA_HttpGet_Request();
		request.setUrl(url);
		request.setCookies(SystemConfig.getCookies());

		Map<String, BaseObject> httpRequests = new HashMap<String, BaseObject>();

		httpRequests.put("request", request);

		ServiceResponse httpGetResponse = SystemConfig.getService()
				.Callservice(httpRequests, "WSOA_HttpGet",
						WSOA_HttpGet_Response.class);

		if (!httpGetResponse.getExeFlag()) {
			showMessage(httpGetResponse.getMessage());
			returnActivity();
			return;
		}
		WSOA_HttpGet_Response response = (WSOA_HttpGet_Response) httpGetResponse
				.getResponseData();
		if (response.getExeFlag() == 0) {
			showMessage(response.getMessage());
			returnActivity();
			return;
		}
		if (response.getExeFlag() == 2) {
			if (SystemConfig.reLogin()) {
				processUrl(url);
				return;
			} else {
				BaseActivity.returnLoginActivity();
				return;
			}
		}
		page = response.getNextPage();

		// 定义一个要展示View
		// 先根据入参数据确定好要生成的BaseView子类
		baseView = BaseView.CreateBaseViewFactory(this, page, url);
		// 使用BaseView子类将要显示的View内容生成，使用DrawSelf方法
		viewGroup = baseView.DrawSelf();
		if (viewGroup == null) {
			returnActivity();
			return;
		}

		// 将要显示的内容放入setContentView
		setContentView(viewGroup);
	}
}
